package com.shujia.custom.udf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

//需求：求每个学生的总分，使用自定义函数，判断每个学生的成绩等级（0-450 差  450-550 良好  550 优）
public class MyUDFDemo4 extends GenericUDF {
    private String output;
    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        output = "";
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }

    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        if(arguments.length>=1){
            int score = Integer.parseInt(arguments[0].get().toString());
            if(0<=score && score<450){
                output = "差";
            }else if(450<=score && score<550){
                output = "良好";
            }else if(score>=550){
                output = "优";
            }
        }
        return output;
    }

    @Override
    public String getDisplayString(String[] children) {
        return "这是自定义函数2";
    }
}
